<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Collections</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="../js/underscore.js"></script>
    <script>
        var obj = {
            name: 'bob',
            school: 'No.1 middle school',
            address: 'xueyuan road'
        };
        //返回值数组
        var upper = _.map(obj, function (value, key) {
            return value.toUpperCase();
        });
        console.log(JSON.stringify(upper));
        //返回转换后的对象
        upper = _.mapObject(obj, function (value, key) {
            return value.toUpperCase();
        });
        console.log(JSON.stringify(upper));

        var intArray = [123, 232, 445, 364, 65, 76, 3465, 778];
        //取最大值
        console.log("max value in " + JSON.stringify(intArray) + " is " + _.max(intArray));
        //取最小值
        console.log("min value in " + JSON.stringify(intArray) + " is " + _.min(intArray));
        var intObj = {
            a: 123, b: 232, c: 445, d: 364, e: 65, f: 76, g: 3465, h: 778
        };
        //取value最大值
        console.log("max value in " + JSON.stringify(intObj) + " is " + _.max(intObj));
        //取value最大值
        console.log("min value in " + JSON.stringify(intObj) + " is " + _.min(intObj));
        var grouped = _.groupBy(intArray, function (x) {
            if (x % 2 == 0) {
                return "Odd";
            } else {
                return "Even";
            }
        });
        //数组按奇偶分组
        console.log(JSON.stringify(intArray) + " groupBy Odd and Even " + JSON.stringify(grouped));
        //随机打乱数组
        console.log(JSON.stringify(intArray) + " shuffle ocne " + JSON.stringify(_.shuffle(intArray)));
        console.log(JSON.stringify(intArray) + " shuffle twice " + JSON.stringify(_.shuffle(intArray)));
        //随机取1个值
        console.log(JSON.stringify(intArray) + " sample 1 " + JSON.stringify(_.sample(intArray)));
        //随机取多个值
        console.log(JSON.stringify(intArray) + " sample 3 " + JSON.stringify(_.sample(intArray, 3)));
    </script>
</head>

<body>
</body>

</html>